
//
//  MJStatusCell.swift
//  新浪微博
//
//  Created by MaJi on 16/4/12.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SnapKit
class MJStatusCell: UITableViewCell {
//记录底部toolBar的top约束
    var toolBarTopCons: Constraint?
//    绑定模型数据
    var statusViewModel:MJststusOriganalVIewModel? {
        didSet{
//            给他们的属性statusViewModel赋值
            originalView.statusViewModel = statusViewModel
//            转发微博
//            retweetedView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            
            //判断是否是转发微博 修改底部toolbar顶部的约束
            //放置cell复用  snp_updateConstraints: 如果约束存在就更新,约束不存在就创建
            
            //在更新约束之前先 卸载toolbar顶部的约束
            self.toolBarTopCons?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                //            是转发微博
                //                3.toolbar顶部的约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
//                1.赋值
                retweetedView.statusViewModel = statusViewModel
//                2.显示
                retweetedView.hidden = false
            } else{
                //                3.toolbar顶部的约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
//                1.赋值
                retweetedView.statusViewModel = nil
//                2.显示
                retweetedView.hidden = true
            }

        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        重写init方法,设置子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
//        backgroundColor = randomColor()
//        设置cell选中的颜色为空
        selectionStyle = .None
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(toolBar)
//        设置自动布局
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            //            make.height.equalTo(100)
            //            底部视图的布局
        retweetedView.snp_makeConstraints(closure: { (make) -> Void in
                make.top.equalTo(originalView.snp_bottom)
                make.left.right.equalTo(originalView)
            })
        toolBar.snp_makeConstraints(closure: { (make) -> Void in
                self.toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
                make.left.right.equalTo(contentView)
                make.height.equalTo(36)
            })
        contentView.snp_makeConstraints(closure: { (make) -> Void in
                //其他的几个约束也需要同时添加 不然contentView只知道底部在哪个地方
                make.top.left.right.equalTo(self)
                make.bottom.equalTo(toolBar.snp_bottom)
            })
        }
    }
//    懒加载原微博视图
    private lazy var originalView:MJStatusOriginalView = MJStatusOriginalView()
    private lazy var retweetedView:MJStatusRetweetedView = MJStatusRetweetedView()
//    懒加载底部toolBar
    private lazy var toolBar:MJStatueToolBar = MJStatueToolBar()
    
}
